﻿/*安装元件拖放及位置检测*/
using UnityEngine;
using UnityEngine.UI;
using Common;
public class FollowMouse : MonoBehaviour
{
    public Transform targetTransform; //元件正确位置
    GameObject ToggleHelp;
    AudioClip success;
    AudioClip fail;
    AudioClip lose;
    AudioClip celebrate;
    Text Notice;
    AssemblyRun assemblyRun;
    Vector3 screenPos;
    // Use this for initialization
    void Start()
    {
        //设置正在安装元件状态
        assemblyRun = GameObject.Find("AssemblyRun").GetComponent<AssemblyRun>();
        assemblyRun.componentIsFit = 1;
        //Debug.Log(assemblyRun.componentIsFit);
        
        string shader = "Legacy Shaders/Transparent/Diffuse";
           
        //元件位置帮助开关
        ToggleHelp = GameObject.Find("ToggleHelp");
        //检测是否需要元件位置帮助
        if (ToggleHelp.GetComponent<Toggle>().isOn)
        {
            //设置目标元件半透明 
            SetRender(targetTransform, shader, 0.5f);
        }
        else
        {
            SetRender(targetTransform, shader, 0f);
        }

        //显示安装元件
        SetRender(this.transform, shader, 1f);

        //添加声音clip
        success = Resources.Load("Audio/success", typeof(AudioClip)) as AudioClip;
        fail = Resources.Load("audio/fail", typeof(AudioClip)) as AudioClip;
        lose = Resources.Load("audio/lose", typeof(AudioClip)) as AudioClip;
        celebrate = Resources.Load("audio/celebrate", typeof(AudioClip)) as AudioClip;
        //获取Notice
        Notice = GameObject.Find("Notice").GetComponent<Text>();
        screenPos = Camera.main.WorldToScreenPoint(targetTransform.transform.position);//获取需要移动物体的目标位置世界转屏幕坐标
    }

    // Update is called once per frame
    void Update()
    {
        
        Vector3 mousePos = Input.mousePosition;
        //因为鼠标只有X，Y轴，所以要赋予给鼠标Z轴
        mousePos.z = screenPos.z;
        //把鼠标的屏幕坐标转换成世界坐标
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
        //控制物体移动
        transform.position = worldPos;

        if (Input.GetMouseButtonDown(1))
        {
            //检测元件与目标位置距离，若小于指定范围则放置正确
            if (Vector3.Distance(transform.position, targetTransform.position) < 0.02f)
        
            {
                //设置目标元件不透明
                string shader = "Legacy Shaders/Transparent/Cutout/Diffuse";
                SetRender(targetTransform, shader, 1f);
                if (assemblyRun.layoutIndex<assemblyRun.layoutComponents.Count)
                {
                    assemblyRun.layoutIndex++;
                        //检测是否需要位置帮助
                        if (ToggleHelp.GetComponent<Toggle>().isOn)
                        {
                        assemblyRun.layoutComponents[assemblyRun.layoutIndex].SetActive(true);
                            string shader1 = "Legacy Shaders/Transparent/Diffuse";
                            SetRender(assemblyRun.layoutComponents[assemblyRun.layoutIndex].transform, shader1, 0.5f);
                        }
                }

                //取消元件正在安装状态
               assemblyRun.componentIsFit= -1;
               Debug.Log(assemblyRun.componentIsFit);
                //元件安装是否完成
                if (assemblyRun.layoutIndex == assemblyRun.layoutComponents.ToArray().Length)
                {
                    MessageBox.Show("提示", "恭喜你，完成全部元件安装！", "去接线");
                    MessageBox.confim = () => {
                            GameObject.Find("AssemblyRun").GetComponent<AssemblyRun>().BtnWiring();
                    };
                }               
                //安装成功声音
                AudioSource.PlayClipAtPoint(success, this.transform.position);
                Notice.text = "安装正确，太棒了，加油呀！";
                GameObject.Destroy(this.transform.gameObject);
            }
            else
            {
                string shader = "Legacy Shaders/Transparent/Diffuse";
                //安装失败声音
                AudioSource.PlayClipAtPoint(fail, this.transform.position);
                //更新安装错误次数及成绩
                Global.LayoutErrorNum++;
                Global.Score = Global.Score-5;

                Notice.text = "安装位置不正确，请重新放置！";
            }
        }
    }

    void SetRender(Transform targetTransform, string shader, float num)
    {
        foreach (Transform child in targetTransform.gameObject.GetComponentsInChildren<Transform>(true))
        {
            Renderer renderer = child.GetComponent<Renderer>();
            if (renderer)
            {
                for (int i = 0; i < renderer.materials.Length; i++)
                {
                    renderer.materials[i].shader = Shader.Find(shader);
                    renderer.materials[i].color = new Color(
                    renderer.materials[i].color.r,
                    renderer.materials[i].color.g,
                    renderer.materials[i].color.b,
                    num);
                }

            }
        }
    }
}
